package com.example.fittrack.utils;

import org.springframework.util.Assert;

import java.util.Collections;
import java.util.List;


public class Page<T> {

    private final Integer currentPage;

    private final Integer pageSize;

    private final Long total;

    private final Integer pages;

    private final Boolean hasNextPage;

    private final Boolean hasCurrentPage;

    /**
     * 起始行, 使用时需要先判断hasCurrentPage,
     * <p>
     * 如果没有当前页，则startRow = -1
     */
    private final Long startRow;
    /**
     * 末行, 使用时需要先判断hasCurrentPage,
     * <p>
     * 如果没有当前页，endRow = -1
     */
    private final Long endRow;

    private List<T> data;

    public Page(int currentPage, int pageSize, long total) {
        Assert.isTrue(currentPage > 0, "参数currentPage需要大于0");
        Assert.isTrue(pageSize > 0, "参数pageSize需要大于0");
        Assert.isTrue(pageSize <= 500, "参数pageSize需要小于等于500");
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        Assert.isTrue(total >= 0, "参数total需要大于等于0");
        this.total = total;
        this.pages = (int) (total / this.pageSize + (total % this.pageSize == 0 ? 0 : 1));
        this.hasNextPage = this.currentPage < this.pages;
        this.hasCurrentPage = this.currentPage <= this.pages;
        if (!hasCurrentPage) {
            this.startRow = -1L;
            this.endRow = -1L;
        } else {
            this.startRow = (long) ((this.currentPage - 1) * pageSize);
            if (currentPage < pages) {
                this.endRow = (long) (this.currentPage * pageSize - 1);
            } else {
                this.endRow = this.total - 1;
            }
        }
        this.data = Collections.emptyList();
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Long getTotal() {
        return total;
    }

    public Integer getPages() {
        return pages;
    }

    public Boolean hasNextPage() {
        return hasNextPage;
    }

    public Boolean hasCurrentPage() {
        return hasCurrentPage;
    }

    public Long getStartRow() {
        return startRow;
    }

    public Long getEndRow() {
        return endRow;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
